// // import TurkishTetris_GameStorage from "../../../../hall/Data/TurkishTetris_GameStorage";

import { _decorator, Component, UITransform } from 'cc';
const { ccclass, property, executionOrder } = _decorator;

import TurkishTetris_CardFunctions from "../Common/TurkishTetris_CardFunctions";
import TurkishTetris_GameConfig from "../../../../hall/Data/TurkishTetris_GameConfig";
import { TurkishTetris_GameData } from "../../../../hall/Data/TurkishTetris_GameData";
import { CardType } from "../../../../hall/Data/TurkishTetris_GameEnum";
import TurkishTetris_Grid from "../Game/TurkishTetris_Grid";
import TurkishTetris_GameManager from "./TurkishTetris_GameManager";
import { TurkishTetris_GameStorage } from "../../../../hall/Data/TurkishTetris_GameStorage";

// /** MapManager */
// // 慢一点执行
@ccclass('TurkishTetrisMapManager')
@executionOrder(102)
export default class TurkishTetris_MapManager extends Component {
        static Instance: TurkishTetris_MapManager;
        onLoad() {
                TurkishTetris_MapManager.Instance = this;
        }
        async initCard() {
                // let testData = [
                //     { x: 0, y: 0, color: 0, type: CardType.Normal, length: 2 },
                //     { x: 0, y: 2, color: 0, type: CardType.Normal, length: 3 },
                // ];

                this.node.destroyAllChildren();
                TurkishTetris_GameData.arrCards = [];
                TurkishTetris_GameData.arrGrids = [];
                this.node.getComponent(UITransform).width = TurkishTetris_GameConfig.W + 0; // this.cardWidth * this.column + 20;
                this.node.getComponent(UITransform).height = TurkishTetris_GameConfig.H + 0; // this.cardHeight * this.row + 20;

                for (let x = 0; x < TurkishTetris_GameConfig.col; x++) {
                        TurkishTetris_GameData.arrGrids[x] = [];
                        TurkishTetris_GameData.arrCards[x] = [];
                        for (let y = 0; y < TurkishTetris_GameConfig.row; y++) {
                                // const id = testData[x][y];
                                const id = 0;
                                const scrGrid: TurkishTetris_Grid = TurkishTetris_CardFunctions.CreateGrid(id, x, y);
                                TurkishTetris_GameData.arrGrids[x][y] = scrGrid;
                        }
                }

                for (let y = 0; y < TurkishTetris_GameConfig.row; y++) {
                        TurkishTetris_GameData.arrCardGroups[y] = [];
                }

                console.log(`TurkishTetris_GameStorage?.data?.isEnd`, TurkishTetris_GameStorage?.data?.isEnd)

                // if (TurkishTetris_GameStorage.data.GameData.IsGameOn && TurkishTetris_GameStorage?.data?.GameData?.mapData && TurkishTetris_GameStorage.data.GameData.mapData.length > 0) {
                if (TurkishTetris_GameStorage?.data?.isEnd) {
                        TurkishTetris_GameManager.NewGame();
                } else {
                        this.MapDataToArrCardGroups();
                        TurkishTetris_GameData.hasNewLine = true;
                        TurkishTetris_GameManager.Main(11);
                }




                // if (TurkishTetris_GameData.gameMode == 0) {
                //     if (TurkishTetris_GameStorage.data.GameData.IsGameOn && TurkishTetris_GameStorage?.data?.GameData?.mapData && TurkishTetris_GameStorage.data.GameData.mapData.length > 0) {
                //         // GameData.curScore = LocalStorage.getInstance().getInt(HallConst.LocalStorageKey.curScore, 0);
                //         this.MapDataToArrCardGroups();
                //         TurkishTetris_GameData.hasNewLine = true;
                //         TurkishTetris_GameManager.Main(11);
                //     } else {
                //         TurkishTetris_GameManager.NewGame();

                //         // CardFunctions.NewLineBlock_next();

                //         // CardFunctions.NewLineBlock(0);
                //         // CardFunctions.NewLineBlock(1);
                //         // CardFunctions.NewLineBlock(2);

                //         // XXL_GameManager.Main();
                //     }
                // } else if (TurkishTetris_GameData.gameMode == 1) {
                //     // let desertGameLevel = TurkishTetris_GameData.getDesertGameLevel();
                //     // let curDesertLevel = TurkishTetris_GameData.getCurDesertLevel();
                //     // await this.tileMapToArrCardGroups(desertGameLevel);
                //     TurkishTetris_GameManager.Main(12);
                //     // XXL_GamePanel.Instance.timeProgress.run({
                //     //     tm: XXL_GameConfig.DESERT_MODE_TIME_CFG[curDesertLevel - 1],
                //     //     timeZeroFun: () => {
                //     //         xwLog("desertGame Time zero !");
                //     //         gameFunc.showPanel(GameResConst.PREFABS_PATH.desertLosePanel);
                //     //     },
                //     // });
                // }



                // CardFunctions.NewLineBlock(3);
                // CardFunctions.NewLineBlock(4);
                // CardFunctions.NewLineBlock(5);
                // CardFunctions.NewLineBlock(6);
                // CardFunctions.NewLineBlock(7);
                // CardFunctions.NewLineBlock(8);
                // CardFunctions.NewLineBlock(9);

                // CardFunctions.AllBlockFallToCanNotFall();
        }
        //    // 把保存下来的地图放到内存中初始化
        MapDataToArrCardGroups() {
                let arrCardGroups = [];

                // console.log("this.data.mapData", this.data.mapData);

                for (let y = 0; y < TurkishTetris_GameStorage.data.GameData.mapData.length; y++) {
                        arrCardGroups[y] = [];
                        const row = TurkishTetris_GameStorage.data.GameData.mapData[y];
                        for (let index = 0; index < row.length; index++) {
                                const element = row[index];
                                const x_start = element.x;
                                const length = element.length;
                                // const x_end = x_start + length;
                                const y = element.y;
                                const id = 0;
                                const color = element.color;
                                const cardType: CardType = element.type;
                                const targetX: CardType = element.targetX;
                                const cardGroup = TurkishTetris_CardFunctions.CreateCardGroup(id, cardType, x_start, y, length, color, targetX);
                                TurkishTetris_GameData.arrCardGroups[y].push(cardGroup);
                        }
                }

                // console.log(`data222`, arrCardGroups);

                for (let index = 0; index < TurkishTetris_GameStorage.data.GameData.mapData_next.length; index++) {
                        const element = TurkishTetris_GameStorage.data.GameData.mapData_next[index];
                        const x_start = element.x;
                        const length = element.length;
                        const y = element.y;
                        const id = 0;
                        const color = element.color;
                        const cardType: CardType = element.type;
                        const targetX: CardType = element.targetX;
                        const cardGroup = TurkishTetris_CardFunctions.CreateCardGroup(id, cardType, x_start, y, length, color, targetX);
                        TurkishTetris_GameData.arrCardGroups_next.push(cardGroup);
                }

                TurkishTetris_GameManager.Instance.floorTipCom.showFloorTip();
                return arrCardGroups;
        }
        //    // async tileMapToArrCardGroups(mapName: string) {
        //    //     let selfResolve: (value) => void;
        //    //     const p = new Promise<[[]]>((resolve) => {
        //    //         selfResolve = resolve;
        //    //     });
        //    //     // 获取tiledMap数组
        //    //     LoaderManager.getInstance().loadRes(
        //    //         `map/${mapName}`,
        //    //         gen_handler((res) => {
        //    //             // xwLog(res);
        //    //             let tileMapNode = new cc.Node();
        //    //             let tiledMap = tileMapNode.addComponent(cc.TiledMap);
        //    //             tiledMap.tmxAsset = res;
        //    //             // 根据图层名获取图层
        //    //             let layer: cc.TiledLayer = tiledMap.getLayer("layer_1");
        //    //             // 获取图层的行列数
        //    //             let layerSize: cc.Size = layer.getLayerSize();
        //    //             let width = layerSize.width;
        //    //             let height = layerSize.height;
        //    //             // console.log(layerSize); // size(width:40, height:30)
        //    //             // 获取图层的gid
        //    //             let mapData = [];
        //    //             for (let i = 0; i < height; i++) {
        //    //                 mapData[i] = [];
        //    //                 for (let j = 0; j < width; j++) {
        //    //                     mapData[i].push(layer.getTileGIDAt(new cc.Vec2(j, i)));
        //    //                 }
        //    //             }
        //    //             // console.log(mapData);
        //    //             tileMapNode.destroy();
        //    //             selfResolve(mapData);
        //    //         }),
        //    //         cc.resources,
        //    //         cc.TiledMapAsset
        //    //     );
        //    //     let tileMapData = await p;
        //    //     let mapData = [[], [], [], [], [], [], [], [], [], []];
        //    //     // 转换数据格式
        //    //     for (let i = tileMapData.length - 1; i >= 0; i--) {
        //    //         for (let j = 0; j < tileMapData[i].length; j++) {
        //    //             const tile = tileMapData[i][j];
        //    //             if (tile > 0) {
        //    //                 let tmp = {
        //    //                     id: 0,
        //    //                     type: 0,
        //    //                     x: j,
        //    //                     y: 9 - i,
        //    //                     length: tile,
        //    //                     color: 5,
        //    //                     targetX: -1,
        //    //                 };
        //    //                 mapData[9 - i].push(tmp);
        //    //             }
        //    //         }
        //    //     }
        //    //     let arrCardGroups = [];
        //    //     for (let y = 0; y < mapData.length; y++) {
        //    //         arrCardGroups[y] = [];
        //    //         const row = mapData[y];
        //    //         for (let index = 0; index < row.length; index++) {
        //    //             const element = row[index];
        //    //             const x_start = element.x;
        //    //             const length = element.length;
        //    //             // const x_end = x_start + length;
        //    //             const y = element.y;
        //    //             const id = 0;
        //    //             const color = element.color;
        //    //             const cardType: CardType = element.type;
        //    //             const targetX: CardType = element.targetX;
        //    //             const cardGroup = CardFunctions.CreateCardGroup(id, cardType, x_start, y, length, color, targetX);
        //    //             GameData.arrCardGroups[y].push(cardGroup);
        //    //         }
        //    //     }
        //    // }
}

// /** 调试 */
window["TurkishTetris_MapManager"] = TurkishTetris_MapManager;